<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 17-12-20
 * Time: 下午1:51
 */

namespace workin\modules\others\models;


use yii\base\Model;
use yii\data\ActiveDataProvider;

class ApiLog extends \common\models\credit_app\ApiLog
{
    public function rules()
    {
        return [
            [['l_id'], 'integer'],
            [['l_action', 'l_params', 'l_dateline'], 'safe'],
        ];
    }

    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    public function search($params)
    {
        $query = ApiLog::find()->alias('b');

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'l_id' => $this->l_id,
        ]);

        $query->andFilterWhere(['like', 'l_action', $this->l_action])
            ->andFilterWhere(['like', 'l_params', $this->l_params]);

        if ( ! is_null($this->l_dateline) && strpos($this->l_dateline, ' - ') !== false ) {
            list($start_date, $end_date) = explode(' - ', $this->l_dateline);
            $query->andFilterWhere(['between', 'b.l_dateline', strtotime($start_date), strtotime($end_date.' 23:59:59')]);
        }

        return $dataProvider;
    }
}